package com.javabi.htmlbuilder.html;

import com.javabi.htmlbuilder.css.CSSDeclarationSet;

/**
 * A Standard HTML Element.
 */
public abstract class HTMLElement<H extends HTMLElement<H>> implements HTMLContent {

	/** The style. */
	private HTMLInlineStyle style = null;

	/**
	 * Set the CSS id.
	 * @param id the id.
	 * @return this element.
	 */
	@SuppressWarnings("unchecked")
	public final H setId(String id) {
		if (style == null) {
			style = new HTMLInlineStyle();
		}
		style.setId(id);
		return (H) this;
	}

	/**
	 * Set the CSS clazz.
	 * @param clazz the clazz.
	 * @return this element.
	 */
	@SuppressWarnings("unchecked")
	public final H setClass(String clazz) {
		if (style == null) {
			style = new HTMLInlineStyle();
		}
		style.setClazz(clazz);
		return (H) this;
	}

	/**
	 * Returns the style.
	 * @return the style.
	 */
	public final CSSDeclarationSet style() {
		if (style == null) {
			style = new HTMLInlineStyle();
		}
		return style.declarations();
	}

	/**
	 * Append the inline style to the given HTML builder.
	 * @param html the HTML builder.
	 */
	protected void appendInlineStyleAttributesTo(HTMLBuilder html) {
		if (style != null) {
			style.appendAttributesTo(html);
		}
	}

	/**
	 * Returns the element name.
	 * @return the element name.
	 */
	public abstract HTMLElementName getElementName();

	@Override
	public final String toString() {
		return appendTo(new HTMLBuilder()).toString();
	}
}
